package com.hanbang.chu_xing_gao_zhao.view_dynamic;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hanbang.chu_xing_gao_zhao.adapter.BannerAdAdapter;
import com.hanbang.chu_xing_gao_zhao.application.Global;
import com.hanbang.chu_xing_gao_zhao.application.HttpInterfaces;
import com.hanbang.chu_xing_gao_zhao.base.MainActivity;
import com.hanbang.chu_xing_gao_zhao.entity.BannerAdData;
import com.hanbang.chu_xing_gao_zhao.entity.DynamicData;
import com.hanbang.chu_xing_gao_zhao.entity.DynamicHorozontalListviewData;
import com.hanbang.chu_xing_gao_zhao.entity.SharedData;
import com.hanbang.chu_xing_gao_zhao.myview.GridViewForScrollView;
import com.hanbang.chu_xing_gao_zhao.utils.JsonHelper;
import com.hanbang.chu_xing_gao_zhao.utils.MyRequestCallBack;
import com.hanbang.chu_xing_gao_zhao.utils.MyToast;
import com.hanbang.chu_xing_gao_zhao.utils.ScreenUtils;
import com.hanbang.chu_xing_gao_zhao.utils.SharedUtil;
import com.hanbang.chu_xing_gao_zhao.utils.StringUtils_2;
import com.hanbang.chu_xing_gao_zhao.utils.SystemTools;
import com.hanbang.chu_xing_gao_zhao.utils.UiUtils;
import com.hanbang.chu_xing_gao_zhao.view_dynamic.MorePopupWindowDynamic.OnClickCallback;
import com.hanbang.chu_xing_gao_zhao.view_public.DynamicCommentActivity;
import com.hanbang.cxgz_2.R;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 动态页面的展示
 */
public class DynamicActivity extends MainActivity {
    private LinearLayout dynamic;

    /**
     * 图片轮播adapter
     */
    private BannerAdAdapter bannerAdAdapter;

    /**
     * 图片轮播集合
     */
    private ArrayList<BannerAdData> imageList;

    /**
     * 主页中的我的关注
     */
    private TextView attention;

    /**
     * 动态列表集合
     */
    private ArrayList<DynamicData> listData;

    /**
     * 适配器
     */
    private ListViewAdapter adapter;

    /**
     * 动态列表
     */
    private PullToRefreshListView dynamicListView;

    /**
     * 图片展示集合
     */
    private ArrayList<DynamicHorozontalListviewData> hListData;

    /**
     * 左侧的菜单
     */
    private Button popupLeftMenu;

    /**
     * 屏幕的宽度
     */
    private int screenWidth;

    /**
     * 发布
     */
    private Button issue;

    /**
     * 搜索
     */
    private ImageView sarech;

    /**
     * listView header
     */
    private View headerView;

    /**
     * 筛选条件
     */
    private int filter_id = 0;

    /**
     * 第几页
     */
    private int pageIndex = 1;

    /**
     * 第页多少条
     */
    private int pageSize = 10;

    ListView lv;

    private int sharedType = 1;
    private int commentType = 2;
    private int praiseType = 3;
    private int attentionType = 4;
    private int attentionCancelType = 5;

    /**
     * 记住分享的位置
     */
    private int mPosition;
    private IntentFilter intentFilter;
    private MyBroadcast myBroadcast;
    private MorePopupWindowDynamic morePopWindow;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dynamic_activity);

        initView();
        setListViewAdapter();
        getHttpDynamicList(-99, pageIndex, pageSize);
        setOnClickListener();
        setUpViewsAndAddLinstener(dynamic);
        initImageCycleVeiw();


    }

    /**
     * 判断左侧菜单不是在显示
     */
    private boolean visibility = true;

    /**
     * 判断是不是第一次进入
     */
    private int count = 1;

    @SuppressLint("NewApi")
    public void toggleMenu(View view) {
        Intent intent = new Intent();
        intent.setClass(DynamicActivity.this, DynamicSideslipActivity.class);
        startActivity(intent);


    }

    /**
     * 初始化view
     */
    private void initView() {
        dynamic = (LinearLayout) findViewById(R.id.bar_botton_bar_dynamic);
        attention = (TextView) findViewById(R.id.tv_bar_top_bar_title);
        listData = new ArrayList<DynamicData>();
        dynamicListView = (PullToRefreshListView) findViewById(R.id.lv_dynamic_activity_list);
        hListData = new ArrayList<DynamicHorozontalListviewData>();

        popupLeftMenu = (Button) findViewById(R.id.dynamic_activity_popup);
        screenWidth = ScreenUtils.getScreenWidth(DynamicActivity.this);

        issue = (Button) findViewById(R.id.btn_bar_top_issue);
        sarech = (ImageView) findViewById(R.id.iv_bar_top_search);

        intentFilter = new IntentFilter();
        intentFilter.addAction(Global.DYNAMIC_SHARED);
        myBroadcast = new MyBroadcast();
        registerReceiver(myBroadcast, intentFilter);

    }

    /**
     * 设置点击事件
     */
    private void setOnClickListener() {
        attention.setOnClickListener(this);
        issue.setOnClickListener(this);
        sarech.setOnClickListener(this);

        // 动态列表上下刷新
        dynamicListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                listData.clear();
                hListData.clear();
                pageIndex = 1;
                getHttpDynamicList(-99, pageIndex, pageSize);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                ++pageIndex;
                getHttpDynamicList(filter_id, pageIndex, pageSize);

            }
        });

        /**
         * 滚动的监听
         */
        dynamicListView.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {
                    case OnScrollListener.SCROLL_STATE_IDLE:
                        popupLeftMenu.setVisibility(View.VISIBLE);
                        break;

                    default:
                        popupLeftMenu.setVisibility(View.GONE);
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub

            }
        });

    }

    /**
     * 图片轮播的数据加载与调用
     */
    private void initImageCycleVeiw() {

        imageList = new ArrayList<BannerAdData>();

        // 初始化广告条 数据完成的时候要调用start
        bannerAdAdapter = new BannerAdAdapter(this,
                (RelativeLayout) headerView.findViewById(R.id.dynamic_activity_pictureCycle), imageList);
        bannerAdAdapter.setWidthAndHeight(16, 9);// 设置图片比例

        imageList
                .add(new BannerAdData(
                        1,
                        "http://d.hiphotos.baidu.com/image/h%3D220/sign=c96cea7b8ed4b31cef3c93b9b7d6276f/0d338744ebf81a4cd28e1df1d22a6059252da64e.jpg"));
        imageList.add(new BannerAdData(1, "http://img0.imgtn.bdimg.com/it/u=3045733783,3374698011&fm=21&gp=0.jpg"));
        imageList.add(new BannerAdData(1, "http://img4.imgtn.bdimg.com/it/u=12375103,1146398241&fm=21&gp=0.jpg"));
        imageList.add(new BannerAdData(1, "http://img5.imgtn.bdimg.com/it/u=2296455973,41150422&fm=21&gp=0.jpg"));

        bannerAdAdapter.notifyDataSetChanged();
        bannerAdAdapter.start();// 启动轮播

    }

    /**
     * 处理监听
     */
    @Override
    public void onClick(View v) {
        super.onClick(v);
        Intent intent = new Intent();
        morePopWindow = new MorePopupWindowDynamic(this, R.layout.item_popupwindow_dynamic_top);
        getHttpCount();

        switch (v.getId()) {
            // 打开我的关注
            case R.id.tv_bar_top_bar_title:
                morePopWindow.showPopupWindow(findViewById(R.id.tv_bar_top_bar_title_praent));

                break;
            // 跳转到发布页面
            case R.id.btn_bar_top_issue:
                intent.setClass(DynamicActivity.this, DynamicIssueActivity.class);
                startActivity(intent);
                break;

            // 搜索页面
            case R.id.iv_bar_top_search:
                intent.setClass(DynamicActivity.this, DynamicSearchActivity.class);
                startActivity(intent);
                break;

            default:
                break;

        }
        /**
         * 标题下拉菜单 item 的监听
         *
         */
        morePopWindow.setOnClickCallback(new OnClickCallback() {

            @Override
            public void onClick(MorePopupWindowDynamic morePopWindow, Object number) {

                switch ((Integer) number) {

                    case R.id.item1:
                        filter_id = 0;
                        listData.clear();
                        hListData.clear();
                        pageIndex = 1;
                        getHttpDynamicList(filter_id, pageIndex, pageSize);

                        adapter.notifyDataSetChanged();
                        setTitle(findViewById(R.id.tv_bar_top_bar_title), "我关注的全部");
                        Toast.makeText(DynamicActivity.this, "我关注的全部", Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.item2:
                        pageIndex = 1;
                        filter_id = 1;
                        listData.clear();
                        hListData.clear();
                        getHttpDynamicList(filter_id, pageIndex, pageSize);
                        adapter.notifyDataSetChanged();
                        setTitle(findViewById(R.id.tv_bar_top_bar_title), "我关注的厨师");
                        Toast.makeText(DynamicActivity.this, "我关注的厨师", Toast.LENGTH_SHORT).show();

                        break;
                    case R.id.item3:
                        pageIndex = 1;
                        filter_id = 2;
                        listData.clear();
                        hListData.clear();
                        getHttpDynamicList(filter_id, pageIndex, pageSize);
                        adapter.notifyDataSetChanged();
                        setTitle(findViewById(R.id.tv_bar_top_bar_title), "我关注的经理人");
                        Toast.makeText(DynamicActivity.this, "我关注的经理人", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.item4:
                        pageIndex = 1;
                        filter_id = 3;
                        listData.clear();
                        hListData.clear();
                        getHttpDynamicList(filter_id, pageIndex, pageSize);
                        adapter.notifyDataSetChanged();
                        setTitle(findViewById(R.id.tv_bar_top_bar_title), "我关注的其他");
                        Toast.makeText(DynamicActivity.this, "我关注的其他", Toast.LENGTH_SHORT).show();

                        break;
                    case R.id.item5:
                        pageIndex = 1;
                        filter_id = -99;
                        listData.clear();
                        hListData.clear();
                        getHttpDynamicList(filter_id, pageIndex, pageSize);
                        adapter.notifyDataSetChanged();
                        setTitle(findViewById(R.id.tv_bar_top_bar_title), "全部");
                        Toast.makeText(DynamicActivity.this, "全部", Toast.LENGTH_SHORT).show();

                        break;
                    case R.id.item6:
                        pageIndex = 1;
                        filter_id = 5;
                        listData.clear();
                        hListData.clear();
                        getHttpDynamicList(filter_id, pageIndex, pageSize);
                        adapter.notifyDataSetChanged();
                        setTitle(findViewById(R.id.tv_bar_top_bar_title), "厨师");
                        Toast.makeText(DynamicActivity.this, "厨师", Toast.LENGTH_SHORT).show();

                        break;
                    case R.id.item7:
                        pageIndex = 1;
                        filter_id = 6;
                        listData.clear();
                        hListData.clear();
                        getHttpDynamicList(filter_id, pageIndex, pageSize);
                        adapter.notifyDataSetChanged();
                        setTitle(findViewById(R.id.tv_bar_top_bar_title), "经理人");
                        Toast.makeText(DynamicActivity.this, "经理人", Toast.LENGTH_SHORT).show();

                        break;
                    case R.id.item8:
                        pageIndex = 1;
                        filter_id = 7;
                        listData.clear();
                        hListData.clear();
                        getHttpDynamicList(filter_id, pageIndex, pageSize);
                        adapter.notifyDataSetChanged();
                        setTitle(findViewById(R.id.tv_bar_top_bar_title), "其他");
                        Toast.makeText(DynamicActivity.this, "其他", Toast.LENGTH_SHORT).show();

                        break;
//                    case R.id.item9:
//                        pageIndex = 1;
//                        filter_id = 8;
//                        listData.clear();
//                        hListData.clear();
//                        getHttpDynamicList(filter_id, pageIndex, pageSize);
//                        adapter.notifyDataSetChanged();
//                        setTitle(findViewById(R.id.tv_bar_top_bar_title), "粉丝最多");
//                        Toast.makeText(DynamicActivity.this, "粉丝最多", Toast.LENGTH_SHORT).show();
//
//                        break;
//                    case R.id.item10:
//                        pageIndex = 1;
//                        filter_id = 9;
//                        listData.clear();
//                        hListData.clear();
//                        getHttpDynamicList(filter_id, pageIndex, pageSize);
//                        adapter.notifyDataSetChanged();
//                        setTitle(findViewById(R.id.tv_bar_top_bar_title), "动态最多");
//                        Toast.makeText(DynamicActivity.this, "动态最多", Toast.LENGTH_SHORT).show();
//
//                        break;
//                    case R.id.item11:
//                        pageIndex = 1;
//                        filter_id = 10;
//                        listData.clear();
//                        hListData.clear();
//                        getHttpDynamicList(filter_id, pageIndex, pageSize);
//                        adapter.notifyDataSetChanged();
//                        setTitle(findViewById(R.id.tv_bar_top_bar_title), "评价最多");
//                        Toast.makeText(DynamicActivity.this, "评价最多", Toast.LENGTH_SHORT).show();
//
//                        break;

                    default:
                        break;

                }

            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 888 && resultCode == RESULT_OK) {

            RequestParams params = new RequestParams();

            params.addBodyParameter("telphone", userData.getTelphone());
            params.addBodyParameter("password",userData.getPassword());

            params.addBodyParameter("text", data.getStringExtra("commentText"));
            params.addBodyParameter("feed_id", data.getIntExtra("feed_id", -1) + "");

            getVisit("提交成功", HttpInterfaces.Feed_comment, params, "提交中...", commentType, data.getIntExtra("position", -1));
        }

    }

    /**
     * 1，为 listView 添加 adapter 2，为listView 添加 header
     */
    private void setListViewAdapter() {
        // 设置刷新模试
        dynamicListView.setMode(Mode.BOTH);
        // 新一个适配器
        adapter = new ListViewAdapter(listData);

        // 设置一个头布局
        AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
                AbsListView.LayoutParams.WRAP_CONTENT);
        headerView = getLayoutInflater().inflate(R.layout.dynamic_activity_header, dynamicListView, false);
        headerView.setLayoutParams(layoutParams);
        // 把listView转换成正常的listView,时行添加头部
        lv = dynamicListView.getRefreshableView();
        lv.addHeaderView(headerView);
        dynamicListView.setAdapter(adapter);

    }

    @Override
    protected void onResume() {
        /**
         * 设置顶部的标题:
         */
        setTitle(findViewById(R.id.tv_bar_top_bar_title), "全部");


        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
        unregisterReceiver(myBroadcast);


    }

    /**
     * 获取动态列表
     */
    private void getHttpDynamicList(int filter_id, int pageIndex, int pageSize) {
        RequestParams params = new RequestParams();
        params.addBodyParameter("telphone", userData.getTelphone());
        params.addBodyParameter("password",userData.getPassword());
        params.addBodyParameter("filter_id", filter_id + "");
        params.addBodyParameter("pageIndex", pageIndex + "");
        params.addBodyParameter("pageSize", pageSize + "");

        httpUtils.send(HttpMethod.POST, HttpInterfaces.GetFeedList, params, new MyRequestCallBack<String>(this,
                "页面加载中...") {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                super.onSuccess(responseInfo);
                try {

                    JSONObject json = new JSONObject(responseInfo.result);

                    int result = json.getInt("Result");

                    if (result == 0) {
                        JSONArray VArray = json.getJSONArray("list");
                        // 如果是空表示是最后页
                        if (VArray.length() == 0) {
                            MyToast.show(DynamicActivity.this, "已经是最后一页", 1);
                            // 停止刷新
                            dynamicListView.onRefreshComplete();
                            return;
                        }
                        JSONArray HArray = json.getJSONArray("list_img");
                        // 把数据从接口解析出来
                        listData.addAll(JsonHelper.parseCollection(VArray, ArrayList.class, DynamicData.class));

                        hListData.addAll(JsonHelper.parseCollection(HArray, ArrayList.class,
                                DynamicHorozontalListviewData.class));

                    } else {
                        MyToast.show(DynamicActivity.this, "页面加载失败", 1);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                // 停止刷新
                dynamicListView.onRefreshComplete();
            }
        });

    }

    /**
     * adapter 设置
     *
     * @author Administrator
     */
    class ListViewAdapter extends BaseAdapter {

        public ArrayList<DynamicData> list = new ArrayList<DynamicData>();

        private int positionShare = -1;
        private int feedIdShare = -1;

        public ListViewAdapter(ArrayList<DynamicData> listData) {

            this.list = listData;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public DynamicData getItem(int position) {

            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            final ViewHolder viewHolder;
            if (convertView == null) {// 是空加载tiem
                convertView = getLayoutInflater().inflate(R.layout.item_dynamic_listview, null);
                UiUtils.applyFont(convertView);
                viewHolder = new ViewHolder();

                // 子控件初始化
                viewHolder.imageId = (ImageView) convertView.findViewById(R.id.im_item_listview_dynamic_imageId);
                viewHolder.vip = (ImageView) convertView.findViewById(R.id.iv_item_dynamic_listview_vip);
                viewHolder.name = (TextView) convertView.findViewById(R.id.tv_item_listview_dynamic_name);
                viewHolder.time = (TextView) convertView.findViewById(R.id.tv_item_listview_dynamic_time);
//                viewHolder.phone = (TextView) convertView.findViewById(R.id.tv_item_listview_dynamic_phone); //手机型号取消显示
                viewHolder.group = (TextView) convertView.findViewById(R.id.tv_item_listview_dynamic_group);
                viewHolder.describe = (TextView) convertView.findViewById(R.id.tv_item_listview_dynamic_describe);
                viewHolder.addressed = (TextView) convertView.findViewById(R.id.tv_item_listview_dynamic_addressed);
                viewHolder.transmit = (TextView) convertView.findViewById(R.id.tv_item_listview_dynamic_share);
                viewHolder.comment = (TextView) convertView.findViewById(R.id.tv_item_listview_dynamic_comment);
                viewHolder.praise = (TextView) convertView.findViewById(R.id.tv_item_listview_dynamic_praise);

                viewHolder.transmitParent = (LinearLayout) convertView.findViewById(R.id.ll_item_dynamic_listview_transmit);
                viewHolder.commentParent = (LinearLayout) convertView.findViewById(R.id.ll_item_dynamic_listview_comment);
                viewHolder.praisetParent = (LinearLayout) convertView.findViewById(R.id.ll_item_dynamic_listview_likecount);
                viewHolder.attentionParent = (LinearLayout) convertView.findViewById(R.id.ll_item_dynamic_listView_attention);


                viewHolder.attentionFriend = (ImageView) convertView
                        .findViewById(R.id.cb_item_dynamic_listView_attention);
                viewHolder.attentionFriendText = (TextView) convertView
                        .findViewById(R.id.tv_item_dynamic_listView_attention_text);

                viewHolder.gridView = (GridViewForScrollView) convertView.findViewById(R.id.item_dynamic_gridView);

                convertView.setTag(viewHolder);// 把现有有item对象存起来
            } else {// 不是空复用

                viewHolder = (ViewHolder) convertView.getTag();
            }

            final DynamicData data = getItem(position);

            // 加载网络图片
            bitmapUtils.display(viewHolder.imageId, SystemTools.getHttpFileUrl(data.getAvatar()));
            viewHolder.name.setText(data.getUser_name());
            // 设置时间
            viewHolder.time.setText(StringUtils_2.getTime(data.getAdd_time()));

            //手机型号取消显示
//            viewHolder.phone.setText(data.getPhone());

            viewHolder.group.setText(String.valueOf(data.getFriendcount()));

            StringBuffer sb = new StringBuffer();
            // 把表情转义显示
            if (data.getText() != null) {
                sb.append(data.getText().toString());

                viewHolder.describe.setText(ImageStringUtils.getEmotionContent(DynamicActivity.this, viewHolder.describe,
                        sb.toString()));

                sb.delete(0, sb.length());
            }
            // 指定一个特殊字符，截取它后面的内容。（服务器返回过来的日期为：**省-**市-**区-**街道），显示时只显示城市+街道。

//            String symbol = "-";
            if (data.getPosition() != null) {
                viewHolder.addressed.setText(data.getPosition());
            }

            viewHolder.transmit.setText(String.valueOf(data.getSharecount()));
            viewHolder.comment.setText(String.valueOf(data.getCommentcount()));
            viewHolder.praise.setText(String.valueOf(data.getLikecount()));
            if (!data.isIs_vip()) {

                viewHolder.vip.setVisibility(View.GONE);
            }

            if (data.getFollow() == 0) {

                viewHolder.attentionFriendText.setText("关注");
                viewHolder.attentionFriend.setSelected(false);

            } else {
                viewHolder.attentionFriendText.setText("已关注");
                viewHolder.attentionFriend.setSelected(true);
            }

            viewHolder.gridView.setFocusable(false);
            viewHolder.gridView.setEnabled(false);
            viewHolder.gridView.setFocusableInTouchMode(false);

            viewHolder.gridView.setAdapter(new GridViewAdapter(data.getId(), data));

            /**
             * 头像点击跳转
             */
            viewHolder.imageId.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(DynamicActivity.this, DynamicManageHome.class);
                    // Bundle bundle=new Bundle();
                    // bundle.putSerializable("imageId", hListData);
                    // intent.putExtras(bundle);
                    startActivity(intent);

                }
            });

            /**
             * 打开分享
             */
            viewHolder.transmitParent.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
//                    SharedUtil.shered(DynamicActivity.this, data.getUser_name(), data.getText(), "http://www.baidu.com", SystemTools.getHttpFileUrl(data.getAvatar()));
                    RequestParams params = new RequestParams();
                    params.addBodyParameter("telphone", userData.getTelphone());
                    params.addBodyParameter("password", userData.getPassword());
                    params.addBodyParameter("feed_id", data.getId() + "");
                    SharedUtil.shered(DynamicActivity.this, new SharedData(data.getUser_name(), data.getText(), "http://www.baidu.com", SystemTools.getHttpFileUrl(data.getAvatar()), params));
                    mPosition = position;

                }
            });


            /**
             * 关注的监听
             */
            viewHolder.attentionParent.setOnClickListener(new OnClickListener() {


                @Override
                public void onClick(View v) {
//

                    RequestParams params = new RequestParams();
                    params.addBodyParameter("telphone", userData.getTelphone());
                    params.addBodyParameter("password",userData.getPassword());
                    params.addBodyParameter("user_id", "" + data.getUid());

//                    MyLog.e("text", "888889999" + "*****" + data.getFollow());

                    //如果已经关注过则直接返回
                    if (data.getFollow() == 0) {
                        getVisit("提交成功", HttpInterfaces.Follow, params, "提交中...",
                                attentionType, position);
                    } else {
                        getVisit("提交成功", HttpInterfaces.Follow_Cancel, params, "提交中...",
                                attentionCancelType, position);
                    }


                }
            });

            /**
             * 赞的监听
             */
            viewHolder.praisetParent.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                    RequestParams params = new RequestParams();

                    params.addBodyParameter("telphone", userData.getTelphone());
                    params.addBodyParameter("password",userData.getPassword());

                    params.addBodyParameter("id", data.getId() + "");
                    params.addBodyParameter("type", 7 + "");
                    getVisit("提交成功", HttpInterfaces.Like_Add, params, "正在提交", praiseType, position);

                }
            });

            /**
             * 评价的监听
             */
            viewHolder.commentParent.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(DynamicActivity.this, DynamicCommentActivity.class);
                    intent.putExtra("position", position);
                    intent.putExtra("feed_id", data.getId());
                    startActivityForResult(intent, 888);

                }
            });

            //点出评论跳入动态正文
            viewHolder.describe.setOnClickListener(new OnClickListener() {
                public ArrayList<DynamicHorozontalListviewData> list = new ArrayList<DynamicHorozontalListviewData>();

                @Override
                public void onClick(View v) {

                    list.clear();
                    for (int i = 0; i < hListData.size(); i++) {

                        if (hListData.get(i).getFeed_id() == listData.get(position).getId()) {

                            list.add(hListData.get(i));
                        }
                    }

                    Intent intent = new Intent(DynamicActivity.this, DynamicBodyActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("imageId", list);

                    bundle.putSerializable("data", data);

                    intent.putExtra("listPosition", position);

                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });


            return convertView;
        }

        class ViewHolder {// 进行缓存保存
            // 子控件主要是里面的数据
            private ImageView imageId;
            private ImageView vip;
            private TextView name;
            private TextView time;
            //            private TextView phone;
            private TextView group;
            private TextView describe;
            private TextView addressed;
            private TextView transmit;// 转发
            private TextView comment;
            private TextView praise;// 赞
            private GridViewForScrollView gridView;
            /**
             * 关注朋友
             */
            private ImageView attentionFriend;
            private TextView attentionFriendText;

            private LinearLayout transmitParent;
            private LinearLayout commentParent;
            private LinearLayout praisetParent;
            private LinearLayout attentionParent;


        }

    }


    /**
     * horizaontal listView adapter
     *
     * @author Administrator
     */
    class GridViewAdapter extends BaseAdapter {
        public ArrayList<DynamicHorozontalListviewData> list = new ArrayList<DynamicHorozontalListviewData>();
        private int itemId;
        private DynamicData user = new DynamicData();


        public GridViewAdapter(int itemId, DynamicData user) {
            this.itemId = itemId;
            this.user = user;

            /**
             * 首页只显示3张图片
             */
            int x = 0;
            for (int i = 0; i < hListData.size(); i++) {

                if (hListData.get(i).getFeed_id() == itemId) {

                    this.list.add(hListData.get(i));
                    x++;
                }

            }

        }

        @Override
        public int getCount() {

            return list.size() > 3 ? 3 : list.size();
        }

        @Override
        public DynamicHorozontalListviewData getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            UiUtils.applyFont(convertView);
            ViewHolder viewHolder;

            if (convertView == null) {// 是空加载
                convertView = getLayoutInflater().inflate(R.layout.item_dycamic_horizontal_listview, null);

                viewHolder = new ViewHolder();

                viewHolder.goodsImage = (ImageView) convertView.findViewById(R.id.iv_item_shoppingcart_recommend_image);

                convertView.setTag(viewHolder);
            } else {// 不是空复用

                viewHolder = (ViewHolder) convertView.getTag();
            }

            DynamicHorozontalListviewData data = getItem(position);

            //

            // 加载网络图片
            bitmapUtils.display(viewHolder.goodsImage, SystemTools.getHttpFileUrl(data.getImg_url()));


            viewHolder.goodsImage.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent(DynamicActivity.this, DynamicBodyActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("imageId", list);

                    bundle.putSerializable("data", user);

                    intent.putExtra("listPosition", position);

                    intent.putExtras(bundle);
                    startActivity(intent);

                }
            });

            return convertView;
        }

        class ViewHolder {// 进行缓存保存
            private int id;
            private ImageView goodsImage; // 产品图片
            private String imageUrl;

        }

    }

    /**
     * @param hintSucceed 成功提示语
     * @param url         访问路径
     * @param params      请求参数
     * @param submit      提交等待提示语
     * @return 请求返回状态 0是成功
     */
    public void getVisit(final String hintSucceed, String url, RequestParams params, String submit, final int type,
                         final int position) {

        httpUtils.send(HttpMethod.POST, url, params, new MyRequestCallBack<String>(this, submit) {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                super.onSuccess(responseInfo);
                try {

                    JSONObject json = new JSONObject(responseInfo.result);

                    int result2 = json.getInt("Result");
                    String msg = json.getString("Msg");


                    // 关注
                    if (type == attentionType && result2 == 0) {

                        /**
                         * 关注此人的所有关注信息
                         */
                        for (int i = 0; i < listData.size(); i++) {
                            if (listData.get(position).getUid() == listData.get(i).getUid()) {
                                listData.get(i).setFollow(1);
                            }
                        }

                        adapter.notifyDataSetChanged();

                    }

//                    // 取消关注
                    if (type == attentionCancelType && result2 == 0) {


                        /**
                         * 取消此人的所有关注信息
                         */
                        for (int i = 0; i < listData.size(); i++) {
                            if (listData.get(position).getUid() == listData.get(i).getUid()) {
                                listData.get(i).setFollow(0);
                            }
                        }

                        adapter.notifyDataSetChanged();

                    }

                    // 赞
                    if (type == praiseType) {
                        if (result2 == 0) {
                            listData.get(position).setLikecount(listData.get(position).getLikecount() + 1);


                            adapter.notifyDataSetChanged();

                        } else {
                            MyToast.show(DynamicActivity.this, "不可重复点赞", 1);
                        }
                    }


                    // 评论
                    if (type == commentType && result2 == 0) {
                        listData.get(position).setCommentcount(listData.get(position).getCommentcount() + 1);
                        adapter.notifyDataSetChanged();
                    }

                    // 分享
                    if (type == sharedType && result2 == 0) {
                        listData.get(position).setSharecount(listData.get(position).getSharecount() + 1);

                        adapter.notifyDataSetChanged();
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        });

    }


    /**
     * 获取下拉列表个数
     */
    public void getHttpCount() {

        RequestParams params = new RequestParams();
        params.addBodyParameter("telphone", userData.getTelphone());
        params.addBodyParameter("password",userData.getPassword());


        httpUtils.send(HttpMethod.POST, HttpInterfaces.GetFeedListGeShu, params, new MyRequestCallBack<String>(this, "") {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                super.onSuccess(responseInfo);
                try {

                    JSONObject json = new JSONObject(responseInfo.result);

                    int result = json.getInt("Result");
                    if (result == 0) {
                        JSONArray dataArray = json.getJSONArray("dt");
                        JSONObject data = dataArray.getJSONObject(0);
                        ((TextView) ((LinearLayout) morePopWindow.conentView.findViewById(R.id.item1)).getChildAt(1)).setText(data.get("withMeAllCount").toString());
                        ((TextView) ((LinearLayout) morePopWindow.conentView.findViewById(R.id.item2)).getChildAt(1)).setText(data.get("withMeChushiCount").toString());
                        ((TextView) ((LinearLayout) morePopWindow.conentView.findViewById(R.id.item3)).getChildAt(1)).setText(data.get("withMeJinLiCount").toString());
                        ((TextView) ((LinearLayout) morePopWindow.conentView.findViewById(R.id.item4)).getChildAt(1)).setText(data.get("withMeqitaCount").toString());
                        ((TextView) ((LinearLayout) morePopWindow.conentView.findViewById(R.id.item5)).getChildAt(1)).setText(data.get("quanbuCount").toString());
                        ((TextView) ((LinearLayout) morePopWindow.conentView.findViewById(R.id.item6)).getChildAt(1)).setText(data.get("ChushiCount").toString());
                        ((TextView) ((LinearLayout) morePopWindow.conentView.findViewById(R.id.item7)).getChildAt(1)).setText(data.get("JinliCount").toString());
                        ((TextView) ((LinearLayout) morePopWindow.conentView.findViewById(R.id.item8)).getChildAt(1)).setText(data.get("qitaCount").toString());
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        });

    }

    /**
     * 广播接收
     */
    class MyBroadcast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            listData.get(mPosition).setSharecount(listData.get(mPosition).getSharecount() + 1);
            adapter.notifyDataSetChanged();

        }
    }

}